package ihis.tms.server.redmine.model;

import ihis.model.IHISConstants;
import ihis.model.Task;
import ihis.tms.server.redmine.db.SqlMapClientManager;

import java.sql.Date;
import java.sql.SQLException;

import com.ibatis.sqlmap.client.SqlMapClient;

public class Issue {
	private int id;
	private Integer project;
	private String subject;
	private Date dueDate;
	private Date createDate;
	private Integer category;
	private Integer status;
	private Integer owner;
	private Integer doneRatio;
	private Date updateDate;
	private String description;
	private Integer estimatedHour;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Integer getProject() {
		return project;
	}
	public void setProject(Integer project) {
		this.project = project;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public Date getDueDate() {
		return dueDate;
	}
	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public Integer getCategory() {
		return category;
	}
	public void setCategory(Integer category) {
		this.category = category;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public Integer getOwner() {
		return owner;
	}
	public void setOwner(Integer owner) {
		this.owner = owner;
	}
	public Date getUpdateDate() {
		return updateDate;
	}
	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Integer getDoneRatio() {
		return doneRatio;
	}
	public void setDoneRatio(Integer doneRatio) {
		this.doneRatio = doneRatio;
	}
	public Integer getEstimatedHour() {
		return estimatedHour;
	}
	public void setEstimatedHour(Integer estimatedHour) {
		this.estimatedHour = estimatedHour;
	}
	
	public Task ConvertToTask( int taskType ) throws SQLException
	{
		Task task = new Task();
		SqlMapClient sqlMap = SqlMapClientManager.GetSqlMapClient();
		task.setTaskId(this.id + "");
		task.setSubject(this.getSubject());
		if(this.getOwner() != null)
		{
			User owner = (User) sqlMap.queryForObject("getUserByID", this.getOwner());
//			task.setOwner(new Member(owner.getLogin(), owner.getFirstname(), owner.getLastname()));
			task.setOwnerId(owner.getLogin());
		}
		task.setProgressRatio(this.getDoneRatio());
		if(this.getStatus() != null)
		{
			switch(this.getStatus())
			{
				case 1:
				case 2:
				case 4:
				case 6:
					task.setStatus(IHISConstants.PROGRESS_STATUS_IN_PROGRESS);
					break;
				case 3:
				case 5:
					task.setStatus(IHISConstants.PROGRESS_STATUS_COMPLETED);
					break;					
			}
		}		
		if(this.getProject() != null)
		{
			task.setProjectId(this.getProject().toString());
		}			
		if(this.getCategory() != null)
		{
			task.setTeamId(this.getCategory().toString());
		}		
		task.setDescription(this.getDescription());
		if(this.getEstimatedHour() != null)
			task.setEstimation(this.getEstimatedHour());
		if(this.getCreateDate() != null)
			task.setStartDate(this.getCreateDate());
		if(this.getDueDate() != null)
			task.setDueDate(this.getDueDate());
//		if(this.getUpdateDate() != null && this.getStatus() != null && (this.getStatus() == 3 || this.getStatus() == 5))
//			task.setDueDate(this.getUpdateDate());	// 110506 h
		task.setType(taskType);
		return task;
	}
	
}
